package com.cxyd.modules.job.task.scanTask;

import com.cxyd.modules.api.dto.RcsResult;
import com.cxyd.modules.wcs.dto.AgvTrkJobDTO;
import com.cxyd.modules.wcs.dto.ApplyPassDTO;
import com.cxyd.modules.wcs.service.AgvTrkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component("post-rcs")
@Slf4j
public class PostRcsTask {
    private final RestTemplate restTemplate;
    private final AgvTrkService agvTrkService;
    private final String rcsUrl = "http://172.20.177.176:8181/rcms/services/rest/hikRpcService/genAgvSchedulingTask";
    private final String passUrl = "http://172.20.177.176:8181/rcms/services/rest/hikRpcService/boxApplyPass";
    //    创建锁，防止并发情况出现
    private Lock lock = new ReentrantLock();

    public PostRcsTask(RestTemplate restTemplate, AgvTrkService agvTrkService) {
        this.restTemplate = restTemplate;
        this.agvTrkService = agvTrkService;
    }

    public void scanAgvTrkByA() {
        lock.lock();
        try {
            final AgvTrkJobDTO record = agvTrkService.getOneRecord("CV01");
            if (record == null) {
                return;
            }
            log.info("获取到[A]区CTU任务:{}", record);

            RcsResult respond = restTemplate.postForObject(rcsUrl, record, RcsResult.class);
            if (respond == null) {
                log.error("上位系统无响应!");
                return;
            }
//            发送成功则处理数据
            log.info("respond:" + respond.getMessage());
            agvTrkService.agvTrkHandle(record, respond);

        } catch (Exception e) {
            log.error("下发[A]区RCS指令异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

    public void scanAgvTrkByB() {
        lock.lock();
        try {
            final AgvTrkJobDTO record = agvTrkService.getOneRecord("CV02");
            if (record == null) {
                return;
            }
            log.info("获取到[B]区CTU任务:{}", record);

            RcsResult respond = restTemplate.postForObject(rcsUrl, record, RcsResult.class);
            if (respond == null) {
                log.error("上位系统无响应!");
                return;
            }
//            发送成功则处理数据
            log.info("respond:" + respond.getMessage());
            agvTrkService.agvTrkHandle(record, respond);

        } catch (Exception e) {
            log.error("下发[B]区RCS指令异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

    public void scanApplyPass() {
        lock.lock();
        try {
            final ApplyPassDTO pass = agvTrkService.getPassRecord();
            if (pass == null) {
                return;
            }
            log.info("获取到ApplyPass任务:{}", pass);

            RcsResult respond = restTemplate.postForObject(passUrl, pass, RcsResult.class);
            if (respond == null) {
                log.error("上位系统无响应!");
                return;
            }
//            发送成功则处理数据
            log.info("respond:" + respond.getMessage());
            agvTrkService.agvPassHandle(pass, respond);

        } catch (Exception e) {
            log.error("下发PASS指令异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }
}
